package pb

import "bytes"

// ActionType specifies the operation type being performed that the Macaroon will validate.
type ActionType int

const (
	// not using iota because these values are persisted in macaroons.
	_ ActionType = 0

	// ActionRead specifies a read operation.
	ActionRead ActionType = 1
	// ActionWrite specifies a write operation.
	ActionWrite ActionType = 2
	// ActionList specifies a list operation.
	//ActionList ActionType = 3
	// ActionDelete specifies a delete operation.
	ActionDelete ActionType = 4
	// ActionProjectInfo requests project-level information.
	//ActionProjectInfo ActionType = 5
)

func (c *License) Allow(bucket string, prefix string, action ActionType) bool {
	switch action {
	case ActionRead:
		if !c.Reads {
			return false
		}
	case ActionWrite:
		if !c.Writes {
			return false
		}
	//case ActionList:
	//	if !c.Lists {
	//		return false
	//	}
	case ActionDelete:
		if !c.Deletes {
			return false
		}
	default:
		return false
	}

	if bytes.Equal([]byte(bucket), c.AllowedPaths.Bucket) &&
		bytes.HasPrefix([]byte(prefix), c.AllowedPaths.EncryptedPathPrefix) {
		return true

	}
	return false
}
